#!/usr/bin/perl
print "hanoi 1:\n";
hanoi(1, "A", "C", "B", \&print_move);

print "\nhanoi 2:\n";
hanoi(2, 'A', 'C', 'B', \&print_move);

print "\nhanoi 3:\n";
hanoi(3, "A", "C", "B", \&print_move);

print "\nhanoi 4:\n";
hanoi(4, 'A', 'C', 'B', \&print_move);

sub hanoi {
	my ($n, $start, $end, $extra, $move) = @_;
	if ($n == 1) {
		$move->(1, $start, $end);
	} else {
		hanoi($n-1, $start, $extra, $end, $move);
		$move->($n, $start, $end);
		hanoi($n-1, $extra, $end, $start, $move);
	}
}

sub print_move {
	my ($disk, $start, $end) = @_;
	print "Move disk #$disk from $start to $end.\n";
}
